package com.song.emp.controller;

import com.song.common.entity.R;
import com.song.debp.domain.Dept;
import com.song.debp.service.DeptService;
import com.song.emp.entity.Emp;
import com.song.emp.service.IEmpService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tonysong
 * @since 2024-04-27
 */
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private IEmpService empService;
//    @Resource
//    private DeptService deptService;

    @GetMapping("/")
    public R  getEmp(){
//        //获取所有员工对象集合
//        List<Emp> emps= this.empService.list();
//        //通过循环遍历对象，获取每个对象的部门id
//        for(Emp emp : emps){
//            Long deptId=emp.getDeptId();
//            //通过部门id查询部门对象
//            Dept dept =this.deptService.getById(deptId);
//            //将部门对象的名称设置到员工对象中
//            emp.setDeptName(dept.getDname());
        return new R(200,"ok",true,this.empService.list());

    }

    @GetMapping("/{id}")
    public R getEmpById(Long id){
        return new R(200,"ok",true,this.empService.getById(id));
    }

    @PostMapping("/")
    public R addEmp(@RequestBody Emp emp){
        return  new R(200,"ok",true,this.empService.save(emp));
    }

    @PutMapping("/")
    public R updateEmp(@RequestBody Emp emp){
        return new R(200,"ok",true,this.empService.updateById(emp));
    }

    @DeleteMapping("/{id}")
    public R deleteEmp(@PathVariable Long id){
        return new R(200,"ok",true,this.empService.removeById(id));
    }

}
